<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="返利平台" prop="platformCode" label-width="100px">
        <el-select v-model="queryParams.platformCode" placeholder="请选择返利平台" clearable size="small">
          <el-option
            v-for="dict in platformCodeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="返利时间" prop="rebateDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.rebateDate"
          type="month"
          value-format="yyyy-MM"
          placeholder="选择返利时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['rebate:rebate:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['rebate:rebate:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['rebate:rebate:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['rebate:rebate:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rebateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="返利平台" align="center" prop="platformCode" :formatter="platformCodeFormat" />
      <el-table-column label="返利时间" align="center" prop="rebateDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rebateDate, '{y}-{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="返利金额" align="center" prop="rebateMoney" />
      <el-table-column label="发票" align="center" width="80" prop="invoiceUrl" >
        <template scope="scope">
          <el-popover placement="right" title="" trigger="hover">
            <img :src="baseUrl + scope.row.invoiceUrl" style="height: 400px;width: 500px">
            <img slot="reference" :src="baseUrl + scope.row.invoiceUrl" :alt="baseUrl + scope.row.invoiceUrl" style="max-height: 50px;max-width: 130px">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['rebate:rebate:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['rebate:rebate:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改平台返佣对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="返利平台" prop="platformCode">
          <el-select v-model="form.platformCode" placeholder="请选择返利平台">
            <el-option
              v-for="dict in platformCodeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="返利时间" prop="rebateDate">
          <el-date-picker clearable size="small"
            v-model="form.rebateDate"
            type="month"
            value-format="yyyy-MM"
            placeholder="选择返利时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="返利金额" prop="rebateMoney">
<!--          <el-input v-model="form.rebateMoney" placeholder="请输入返利金额" />-->
          <el-input-number v-model="form.rebateMoney" :precision="2" :step="0.01" :min="0" :max="1000000" placeholder="请输入返利金额" />
        </el-form-item>
        <el-form-item label="发票">
          <imageUpload v-model="imageUrl" v-on:returnData="returnData"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="isSubmintForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRebate, getRebate, delRebate, addRebate, updateRebate, exportRebate } from "@/api/rebate/rebate";
import ImageUpload from '@/components/ImageUpload';

export default {
  name: "Rebate",
  components: {
    ImageUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // baseUrl: process.env.VUE_APP_BASE_API,
      baseUrl: "https://youhuiniu.oss-cn-beijing.aliyuncs.com/",
      imageUrl: '',
      // 总条数
      total: 0,
      // 平台返佣表格数据
      rebateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 返利平台编码字典
      platformCodeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        platformCode: null,
        rebateDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        platformCode: [
          { required: true, message: "返利平台不能为空", trigger: "change" }
        ],
        rebateDate: [
          { required: true, message: "返利时间不能为空", trigger: "blur" }
        ],
        rebateMoney: [
          { required: true, message: "返利金额不能为空", trigger: "blur" }
        ],
        invoiceUrl: [
          { required: true, message: "发票URL地址不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("manage_platform_rebate").then(response => {
      this.platformCodeOptions = response.data;
    });
  },
  methods: {
    /** 查询平台返佣列表 */
    getList() {
      this.loading = true;
      listRebate(this.queryParams).then(response => {
        this.rebateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 返利平台编码字典翻译
    platformCodeFormat(row, column) {
      return this.selectDictLabel(this.platformCodeOptions, row.platformCode);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        platformCode: null,
        rebateDate: null,
        rebateMoney: null,
        invoiceUrl: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.imageUrl = '';
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加平台返佣";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRebate(id).then(response => {
        this.form = response.data;
        this.imageUrl = this.baseUrl + this.form.invoiceUrl;
        this.open = true;
        this.title = "修改平台返佣";
      });
    },
    isSubmintForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.$confirm('确认输入金额是否跟发票金额一致?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return true;
          }).then(() => {
            this.submitForm();
          })
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.form.id != null) {
        updateRebate(this.form).then(response => {
          this.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });
      } else {
        addRebate(this.form).then(response => {
          this.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delRebate(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有平台返佣数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportRebate(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    returnData(res) {
      this.form.invoiceUrl = res.data.objectID;
      this.imageUrl = res.data.url;
    }
  }
};
</script>
